package com.kehao.leetcode.jzoffer;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Kehao.Chen
 * @version 1.0
 * @date 2022/6/28 17:21
 */
public class JZSolution037 {
    public int[] asteroidCollision(int[] asteroids) {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < asteroids.length; i++) {
            list.add(asteroids[i]);
        }
        boolean collision;
        do{
            collision = false;
            for (int i = list.size()-1; i > 0; i--) {
                Integer dataRight = list.get(i);
                Integer dateLeft = list.get(i - 1);
                boolean directionRight = dataRight ==Math.abs(dataRight);
                boolean directionLeft = dateLeft ==Math.abs(dateLeft);
                if(directionLeft && !directionRight){
                    collision = true;
                    if(Math.abs(dataRight)>Math.abs(dateLeft)){
                        list.remove(i-1);
                    }else if(Math.abs(dataRight)<Math.abs(dateLeft)){
                        list.remove(i);
                    }else {
                        list.remove(i);
                        list.remove(i-1);
                        i--;
                    }
                }
            }
        }while (collision);
        return list.stream().mapToInt(i->i).toArray();
    }

    @Test
    public void test01(){
        int[] asteroids = {5,10,-5};
        int[] result = asteroidCollision(asteroids);
        Arrays.stream(result).forEach(i-> System.out.print(i+" "));
    }

    @Test
    public void test02(){
        int[] asteroids = {8,-8};
        int[] result = asteroidCollision(asteroids);
        Arrays.stream(result).forEach(i-> System.out.print(i+" "));
    }

    @Test
    public void test03(){
        int[] asteroids = {10,2,-5};
        int[] result = asteroidCollision(asteroids);
        Arrays.stream(result).forEach(i-> System.out.print(i+" "));
    }

    @Test
    public void test04(){
        int[] asteroids = {-2,-1,1,2};
        int[] result = asteroidCollision(asteroids);
        Arrays.stream(result).forEach(i-> System.out.print(i+" "));
    }

    @Test
    public void test05(){
        int[] asteroids = {-2,-2,1,-1};
        int[] result = asteroidCollision(asteroids);
        Arrays.stream(result).forEach(i-> System.out.print(i+" "));
    }
}
